﻿using System;
using System.Collections.Generic;
using System.Text;
//必须引用以下命名空间，该命名空间提供DllImport方法
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace DLLTest.OpertionClass
{
    class iniOperate
    {
        /*
         *  INI文件其实是一种具有特定结构的文本文件，它的构成分为三部分，结构如下：
         *  [Section1]
         *  key 1 = value2
         *  key 1 = value2
         *  ……
         *  [Section2]
         *  key 1 = value1
         *  key 2 = value2
         */
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string defVal, StringBuilder retVal, int size, string filePath);
        /*读操作
         * section：要读取的段落名
         * key: 要读取的键
         * defVal: 读取异常的情况下的缺省值
         * retVal: key所对应的值，如果该key不存在则返回空值
         * size: 值允许的大小
         * filePath: INI文件的完整路径和文件名
         */
        
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        /*写操作：
         * section: 要写入的段落名
         * key: 要写入的键，如果该key存在则覆盖写入
         * val: key所对应的值
         * filePath: INI文件的完整路径和文件名  
         */
        protected string strDPath = null;
        protected string srcpath = null;
        protected string Path;
        
        /// <summary>
        /// 设置ini文件路径，并保存该路径，为编辑该文件做准备
        /// </summary>
        public void IniFile()
        {            
            this.Path=ScrPach();
        }
        /// <summary>
        /// 直接获取ini文件路径
        /// </summary>
        /// <returns></returns>
        private string ScrPach()
        {           
            //读取ini文件路径
            try
            {
                strDPath = Application.StartupPath + "\\DataBase\\db_VWMS.ini"; ;
                srcpath = strDPath.Substring(0, strDPath.LastIndexOf("\\")).Substring(0, strDPath.Substring(0, strDPath.LastIndexOf("\\")).LastIndexOf("\\")) + "\\DataBase\\db_VWMS.ini";
            }
            catch { }
            return srcpath;
        }
        #region 声明读写INI文件的API函数
        /// <summary>
        /// 写INI文件
        /// </summary>
        /// <param name="section">段落</param>
        /// <param name="key">键</param>
        /// <param name="iValue">值</param>
        public void IniWriteValue(string section, string key, string iValue)
        {
            WritePrivateProfileString(section, key, iValue, this.Path);
        }

        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="section">段落</param>
        /// <param name="key">键</param>
        /// <returns>返回键值</returns>
        public string IniReadValue(string section, string key)
        {
            StringBuilder temp = new StringBuilder();
            int i = GetPrivateProfileString(section, key, "", temp, 255, this.Path);
            return temp.ToString();
        }

        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="section">段，格式[]</param>
        /// <param name="key">键</param>
        /// <returns>返回byte类型的section组或键值组</returns>
        //public byte[] IniReadValues(string section, string key)
        //{
        //    byte[] temp = new byte[255];
        //    int i = GetPrivateProfileString(section,key,"",temp;255,this.Path);
        //    return temp;
        //}
        #endregion
        
    }
}
